//
//  CZModalViewController.swift
//  GZWeibo666
//
//  Created by wu on 16/05/1.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit

import SVProgressHUD

class CZModalViewController: UIViewController,UIScrollViewDelegate, UITextViewDelegate
{
    override func viewDidLoad()
    {
        super.viewDidLoad()
        view.backgroundColor = UIColor.whiteColor()
        setupHeaderTitle(titleLabel.text!)
//        bgView.addSubview(textView)
        
        view.addSubview(textView)
    }
    
    init(titleLabel: UILabel?, userLabel: UILabel?)
    {
        super.init(nibName: nil, bundle: nil)
    }

    required init?(coder aDecoder: NSCoder)
    {
        fatalError("init(coder:) has not been implemented")
    }
    
    func scrollViewDidScroll(scrollView: UIScrollView)
    {
        if !textView.editable
        {
            return
        }
        textView.endEditing(true)
    }
    
    
    //添加
    //设置头部标题和头部按钮
    func setupHeaderTitle(title: String)
    {
        navigationItem.titleView = titleView
//        navigationItem.titleView?.layoutIfNeeded()
        navigationItem.leftBarButtonItem = UIBarButtonItem(title: "取消", style: UIBarButtonItemStyle.Plain, target: self, action: "cancelBtnClick")
        navigationItem.rightBarButtonItem = UIBarButtonItem(title: "发送", style: UIBarButtonItemStyle.Plain, target: self, action: "sendBtnClick")
    }
    
    
    func cancelBtnClick()
    {
        print("点击了取消")
        dismissViewControllerAnimated(true) { () -> Void in
            
        }
    }
    
    func sendBtnClick()
    {
        print("点击了发送按钮")
        
        SVProgressHUD.showInfoWithStatus("正在努力发送中...", maskType: SVProgressHUDMaskType.Black)
        //先取的label里面的内容
        let label = textView.text
        print("评论：\(label)")
        
        if label.isEmpty
        {
            SVProgressHUD.showErrorWithStatus("不能为空！！", maskType: SVProgressHUDMaskType.Clear)
            return
        }
        
        if isRepost
        {
            //转发
            CZRepostStatuses.loadRepostStatuses(repostStatusId, status: label) { (respostStatus, error) -> () in
                
                if error == nil 
                {
                    SVProgressHUD.showSuccessWithStatus("转发成功")
                    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (Int64)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), { () -> Void in
                        
                        self.dismissViewControllerAnimated(true, completion: nil)
                    })
                }
            }
        }
        else
        {
            //评论
            CZCommentStatus.loadCommentStatuses(repostStatusId, comment: label) { (respostStatus, error) -> () in
                
                if error == nil
                {
                    SVProgressHUD.showSuccessWithStatus("评论成功")
                    
                    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (Int64)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), { () -> Void in
                        
                        self.dismissViewControllerAnimated(true, completion: nil)
                    })
                }
            }
        }
    }
    
    //懒加载控件
    lazy var titleView: UIView =
    {
        let bgView = UIView()
        bgView.frame = CGRectMake(0, 0, 120, 44)
        
//        bgView.backgroundColor = UIColor.orangeColor()
        
        //添加子控件
        self.titleLabel.frame.size.width = 60
        self.titleLabel.font = UIFont.systemFontOfSize(13)
        self.titleLabel.textColor = UIColor.blackColor()
        self.titleLabel.sizeToFit()
        bgView.addSubview(self.titleLabel)
        self.titleLabel.textAlignment = NSTextAlignment.Center
        
        self.userLabel.frame.size.width = 60
        self.userLabel.font = UIFont.systemFontOfSize(11)
        self.userLabel.textColor = UIColor.grayColor()
        self.userLabel.sizeToFit()
        bgView.addSubview(self.userLabel)
        self.userLabel.textAlignment = NSTextAlignment.Center
        
        
//        self.userLabel.frame.origin = CGPoint(x: 0, y: 25)
//        self.titleLabel.frame.origin = CGPoint(x: 2, y: 6)
//        self.userLabel.frame.origin.x = view.center.x
//        self.titleLabel.frame.originx = view.center.x
        
        self.userLabel.center.x = bgView.center.x
        self.titleLabel.center.x = bgView.center.x
        self.userLabel.center.y = bgView.bounds.height - self.userLabel.bounds.height - 2
        self.titleLabel.center.y = 10
        
//        bgView.addConstraint(NSLayoutConstraint(item: self.titleLabel, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: bgView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 10))
//        bgView.addConstraint(NSLayoutConstraint(item: self.userLabel, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: bgView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
//        view.addConstraint(NSLayoutConstraint(item: self.userLabel, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 10))
//        view.addConstraint(NSLayoutConstraint(item: self.titleLabel, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: self.userLabel, attribute: NSLayoutAttribute.Top, multiplier: 1, constant: 10))

        return bgView
    }()
    
    
    lazy var titleLabel: UILabel = UILabel()
    lazy var userLabel: UILabel = UILabel()

    //设置评论或者是转发的文本输入框
    lazy var textView: UITextView =
    {
        let textView = UITextView()
        
        textView.frame = CGRectMake(0, 0, UIScreen.mainScreen().bounds.width, UIScreen.mainScreen().bounds.height)
        textView.backgroundColor = UIColor(red: 211/255.0, green: 211/255.0, blue: 211/255.0, alpha: 0.9)
        textView.font = UIFont.systemFontOfSize(18)
        textView.tintColor = UIColor.blackColor()
        textView.showsVerticalScrollIndicator = false
        
//        textView.contentOffset = CGPointMake(0, 0)
        textView.contentInset = UIEdgeInsetsMake(1, 0, 1, 0)
        
        //设置减速的速率
        textView.decelerationRate = 0.001
        
        textView.alwaysBounceVertical = true
        
//        textView.scrollsToTop = true
        
//        textView.selectable = true
        textView.scrollEnabled = true
        textView.delegate = self
//        //自适应高度
//        textView.autoresizingMask = UIViewAutoresizing.FlexibleHeight
        
//        textView.scrollIndicatorInsets = UIEdgeInsetsMake(10, 0, 10, 0)
//        textView.scrollsToTop = true
//        textView.bounces = false
//        textView.showsVerticalScrollIndicator = true
        
        //滚动视图的滚动的范围
        textView.contentSize = CGSizeMake(UIScreen.mainScreen().bounds.width, UIScreen.mainScreen().bounds.height + 5)

        
        //键盘 (inputView)
        var view: UIView = UIView()
        view.backgroundColor = UIColor.redColor()
//        view.frame = CGRectMake(100, 100, 50, 50)
//        textView.inputView = view
        
//        textView.scrollRangeToVisible(NSMakeRange(0, 100))
//        public var inputView: UIView?
//        public var inputAccessoryView: UIView?
//        textView.inputView = view
//        textView.inputAccessoryView = view
        
        
        return textView
    }()
    
//    //懒加载背景view
//    lazy var bgView: UIView =
//    {
//        let view = UIView(frame: CGRectMake(0, 0, UIScreen.mainScreen().bounds.width, UIScreen.mainScreen().bounds.height - 44 - 20))
//        view.backgroundColor = UIColor.redColor()
//        
//        return view
//    }()
    
    
    //微博模型
    var status : CZStatus?
    
    //微博数组
    var statuses: [CZStatus]?
    
    //要转发的用户的ID
    var repostStatusId: Int = 0
    
    //判断是点击了转发按钮还是评论的按钮
    var isRepost: Bool = false
}
